package com.gs.zsmanage.service.impl;

import com.gs.zsmanage.dao.CustomMenuMapper;
import com.gs.zsmanage.service.CustomMenuService;
import com.gs.zsmanage.vo.MenuTwo;
import com.gs.zsmanage.vo.MenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomMenuServiceImpl implements CustomMenuService {

    @Autowired
    CustomMenuMapper customMenuMapper;

    @Override
    public Map getMenusAndIdsByDepartment(String department) {
        List<MenuVO> menuVOS = new ArrayList<>();
        Map map = new HashMap();
        List<String> menuOnes = customMenuMapper.listCustomMenuOnesByDepartment(department);
        List<Integer> customIds = new ArrayList<>();
        if (menuOnes != null)
            for (String menuOne : menuOnes) {
                List<MenuTwo> menuTwos = customMenuMapper.listCustomMenuTwoAndUrls(department, menuOne);
                if (menuTwos != null)
                    for (MenuTwo menuTwo : menuTwos) {
                        customIds.add(menuTwo.getId());
                    }
                MenuVO menuVO = new MenuVO(menuOne, menuTwos);
                menuVOS.add(menuVO);
            }
        map.put("customIds", customIds);
        map.put("menuVOS", menuVOS);
        return map;
    }

    @Override
    public void addDepartmentMenu(String department, Integer menuId) {
        customMenuMapper.addDepartmentMenu(department, menuId);
    }

    @Override
    public void deleteDepartmentMenu(String department, Integer menuId) {
        customMenuMapper.deleteDepartmentMenu(department, menuId);
    }

    @Override
    public List<MenuVO> getAllMenus() {
        List<MenuVO> menuVOS = new ArrayList<>();
        List<String> menuOnes = customMenuMapper.listAllMenuOnes();
        List<Integer> customIds = new ArrayList<>();
        if (menuOnes != null)
            for (String menuOne : menuOnes) {
                List<MenuTwo> menuTwos = customMenuMapper.listAllMenuTwoAndUrls(menuOne);
                if (menuTwos != null)
                    for (MenuTwo menuTwo : menuTwos) {
                        customIds.add(menuTwo.getId());
                    }
                MenuVO menuVO = new MenuVO(menuOne, menuTwos);
                menuVOS.add(menuVO);
            }
        return menuVOS;
    }
}
